package com.example.roundcornerprogressbar.slice;

import com.example.roundcornerprogressbar.ResourceTable;
import com.example.roundcornerprogressbar.view.progressbar.indeterminate.IndeterminateCenteredRoundCornerProgressBar;
import com.example.roundcornerprogressbar.view.progressbar.indeterminate.IndeterminateRoundCornerProgressBar;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.global.resource.ResourceManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class IndeterminateFraction extends Fraction {
    private HiLogLabel label = new HiLogLabel(HiLog.LOG_APP,1, "RoundCorner:IndeterminateFraction");
    private ResourceManager resManager;
    private Component component;
    private IndeterminateRoundCornerProgressBar roundCornerProgressBar;
    private IndeterminateRoundCornerProgressBar roundCornerProgressBar2;
    private IndeterminateCenteredRoundCornerProgressBar centeredRoundCornerProgressBar;
    private IndeterminateCenteredRoundCornerProgressBar centeredRoundCornerProgressBar2;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        component = scatter.parse(ResourceTable.Layout_ability_indeterminate, container, false);
        resManager = component.getResourceManager();
        initView();
        return component;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }

    @Override
    protected void onActive() {
        HiLog.info(label, "onActivity");
        roundCornerProgressBar.startIndeterminateAnimation();
        roundCornerProgressBar2.startIndeterminateAnimation();
        centeredRoundCornerProgressBar.startIndeterminateAnimation();
        centeredRoundCornerProgressBar2.startIndeterminateAnimation();
    }

    @Override
    protected void onInactive() {
        HiLog.info(label, "onInactive");
        roundCornerProgressBar.stopIndeterminateAnimation();
        roundCornerProgressBar2.stopIndeterminateAnimation();
        centeredRoundCornerProgressBar.stopIndeterminateAnimation();
        centeredRoundCornerProgressBar2.stopIndeterminateAnimation();
    }

    private void initView() {
        roundCornerProgressBar = (IndeterminateRoundCornerProgressBar) component.findComponentById(ResourceTable.Id_progressBarIndeterminate);
        if (null != roundCornerProgressBar) {
            setProgressBarColor(roundCornerProgressBar);
        }

        roundCornerProgressBar2 = (IndeterminateRoundCornerProgressBar) component.findComponentById(ResourceTable.Id_progressBarIndeterminate2);
        if (null != roundCornerProgressBar2) {
            setProgressBarColor(roundCornerProgressBar2);
        }
        roundCornerProgressBar2.setAnimationSpeedScale(3);

        centeredRoundCornerProgressBar = (IndeterminateCenteredRoundCornerProgressBar) component.findComponentById(ResourceTable.Id_progressBarIndeterminateCentered);
        if (null != centeredRoundCornerProgressBar) {
            setProgressBarColor(centeredRoundCornerProgressBar);
        }
        centeredRoundCornerProgressBar.setAnimationSpeedScale((float) 0.5);

        centeredRoundCornerProgressBar2 = (IndeterminateCenteredRoundCornerProgressBar) component.findComponentById(ResourceTable.Id_progressBarIndeterminateCentered2);
        if (null != centeredRoundCornerProgressBar2) {
            setProgressBarColor(centeredRoundCornerProgressBar2);
        }
    }

    private void setProgressBarColor(IndeterminateRoundCornerProgressBar roundCornerProgressBar) {
        try {
            roundCornerProgressBar.setProgressBackgroundColor(resManager.getElement(ResourceTable.Color_sample_progress_background).getColor());
            roundCornerProgressBar.setProgressColor(resManager.getElement(ResourceTable.Color_sample_progress_primary).getColor());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setProgressBarColor(IndeterminateCenteredRoundCornerProgressBar roundCornerProgressBar) {
        try {
            roundCornerProgressBar.setProgressBackgroundColor(resManager.getElement(ResourceTable.Color_sample_progress_background).getColor());
            roundCornerProgressBar.setProgressColor(resManager.getElement(ResourceTable.Color_sample_progress_primary).getColor());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
